﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness.Model;
using Bussiness.Service.Factory;

namespace WEB.Admin.include {
    public partial class EditCategory : System.Web.UI.UserControl {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IList<Category> rootCategorys = CategoryServiceUtil.FindRootCategory();
                CategoryDL.Items.Clear();
                CategoryDL.Items.Add(new ListItem());
                foreach (var rootCategory in rootCategorys)
                {
                    CategoryDL.Items.Add(new ListItem(rootCategory.Name, rootCategory.Id.ToString()));
                }
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            var name = CategoryName.Text;
            var des = Description.Text;
            var resourceKey = ResourceKey.Text;
            var category = new Category(name, des,resourceKey);
            if (!String.IsNullOrEmpty(CategoryDL.SelectedValue))
            {
                category.Parent = CategoryServiceUtil.FindCategory(Int64.Parse(CategoryDL.SelectedValue));
            }

            CategoryServiceUtil.AddCategory(category);
        }
    }
}